package org.suspi.skarabee.monitoring;
import java.util.ArrayList;

import com.google.zxing.oned.rss.FinderPattern;
import com.merge.skarabee.R;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass. Activities that
 * contain this fragment must implement the
 * {@link NodeFragment.OnFragmentInteractionListener} interface to handle
 * interaction events. Use the {@link NodeFragment#newInstance} factory method
 * to create an instance of this fragment.
 * 
 */
public class NodeFragment extends Fragment {

	
	private MHandler handler;
	private ArrayList<Integer> configList;
	private View view;
	private ArrayList<ImageButton> buttonList;
	private ArrayList<TextView> portIdList;
	
    @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        LayoutInflater lf = getActivity().getLayoutInflater();   
        view =  lf.inflate(R.layout.fragment_node, container, false);
        handler = new MHandler();
        configList = new ArrayList<Integer>();
        configList.add(R.drawable.conf_0);
        configList.add(R.drawable.conf_1);
        configList.add(R.drawable.conf_2);//
        configList.add(R.drawable.conf_3);
        configList.add(R.drawable.conf_4);
        configList.add(R.drawable.conf_5);
        configList.add(R.drawable.conf_6);
        configList.add(R.drawable.conf_7);
        configList.add(R.drawable.conf_8);
        configList.add(R.drawable.conf_9);
        configList.add(R.drawable.conf_10);
        configList.add(R.drawable.conf_11);
        configList.add(R.drawable.conf_12);
        configList.add(R.drawable.conf_13);
        configList.add(R.drawable.conf_14);
        buttonList = new ArrayList<ImageButton>();
        buttonList.add((ImageButton) view.findViewById(R.id.buttonPort1));
        buttonList.add((ImageButton) view.findViewById(R.id.buttonPort2));
        buttonList.add((ImageButton) view.findViewById(R.id.buttonPort3));
        buttonList.add((ImageButton) view.findViewById(R.id.buttonPort4));
        portIdList = new ArrayList<TextView>();
        portIdList.add((TextView) view.findViewById(R.id.portIdTV1));
        portIdList.add((TextView) view.findViewById(R.id.portIdTV2));
        portIdList.add((TextView) view.findViewById(R.id.portIdTV3));
        portIdList.add((TextView) view.findViewById(R.id.portIdTV4));
        return view;
    }

    public void setData(Bundle bundle){
 	   notifyMessage(bundle);
    }

 	private class MHandler extends Handler {
 		@Override
 		public void handleMessage(Message msg) {
 			Bundle bundle = msg.getData();
 			if(bundle.containsKey("config")) {
 				String value = bundle.getString("config");
 				Integer i = Integer.parseInt(value);
 				FrameLayout f = (FrameLayout) view.findViewById(R.id.frameConfig);
 				f.setBackgroundResource(configList.get(i));
 			}
 			if(bundle.containsKey("connected")){
 				boolean[] arr = bundle.getBooleanArray("connected");
 				for(int i = 0; i < 4; i++){
 					if (arr[i]){
 						buttonList.get(i).setImageDrawable(getResources().getDrawable(R.drawable.port_conn));
 					}else{
 						buttonList.get(i).setImageDrawable(getResources().getDrawable(R.drawable.port_disconn));
 					}
 				}
 			}
 			if(bundle.containsKey("pId")){
 				String[] arr = bundle.getStringArray("pId");
 				for(int i = 0; i < 4; i++){
 					portIdList.get(i).setText("P1 ID: " + arr[i]);
 				}
 			}
 		}
 	}

 	private void notifyMessage(Bundle b){
 		Message msg = handler.obtainMessage();
 		msg.setData(b);
 		handler.sendMessage(msg);
 	}

	/**
	 * @return the view
	 */
	public View getView() {
		return view;
	}

}
